package com.huangliusong.cotroller;

import com.huangliusong.dao.CourseDap;
import com.huangliusong.entity.Course;
import com.huangliusong.entity.CourseList;
import com.huangliusong.entity.CourseMap;
import com.huangliusong.entity.CourseSet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class DataBindController {

    @Resource
    private CourseDap courseDap;

    @RequestMapping("/baseType")
    @ResponseBody
    public String baseType(@RequestParam(value = "id") int id) {
        return "id:" + id;
    }

    @RequestMapping("/packageType")
    @ResponseBody
    public String packageType(@RequestParam(value = "id") Integer id) {

        return "id:" + id;
    }

    @RequestMapping("/arrayType")
    @ResponseBody
    public String arrayType(String[] name) {
        StringBuffer sb = new StringBuffer();
        for (String itrm : name) {
            sb.append(itrm).append(" ");
        }
        return sb.toString();
    }

    @RequestMapping("/pojoType")
    public ModelAndView pojoType(Course course) {
        ModelAndView modelAndView = new ModelAndView();
        courseDap.add(course);
        modelAndView.addObject("courses", courseDap.getAll());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/listType")
    public ModelAndView listType(CourseList courseList) {
        for (Course course : courseList.getList()) {
            courseDap.add(course);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("courses", courseDap.getAll());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/mapType")
    public ModelAndView mapType(CourseMap map) {
        for (String key : map.getCourses().keySet()) {
            Course course = map.getCourses().get(key);
            courseDap.add(course);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("courses", courseDap.getAll());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/setType")
    public ModelAndView setType(CourseSet courseSet) {
        for (Course course : courseSet.getCourses()) {
            courseDap.add(course);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("courses", courseDap.getAll());
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/jsonType")
    @ResponseBody
    public Course jsonType(@RequestBody Course course) {
        course.setPrice(course.getPrice() + 100);
        return course;
    }
}
